using System;
using System.Collections.Generic;
using System.Linq;
using axCommerce.Data.Interface;

namespace axCommerce.Data
{
	public partial class EmailTemplate : ISimpleData
	{
		public EmailTemplateType EmailTemplateTypeInfo { get; set; }

		private List<FileAttach> _fileAttachements;
		public List<FileAttach> FileAttachements
		{
			get
			{
				_fileAttachements = new List<FileAttach>();
				if (EmailTemplateId != null)
					_fileAttachements.AddRange(FileAttach.ListByEmailTemplateId(EmailTemplateId.Value));

				return _fileAttachements;
			}
		}

		private void FillEmailTemplateTypeInfo()
		{
			if (EmailTemplateTypeId.HasValue)
				EmailTemplateTypeInfo = EmailTemplateType.Get(EmailTemplateTypeId.Value);
		}

		public static List<EmailTemplate> GetAllEmailTemplates()
		{
			return Search(new EmailTemplate
			              	{
			              		IsDeleted = false
			              	});
		}

		public static EmailTemplate GetEmailTemlateByTypeCode(int emailTemplateTypeCode)
		{
			return GetAllEmailTemplates().FirstOrDefault(emailTemplate => emailTemplate.EmailTemplateTypeInfo.EmailTemplateTypeCode == emailTemplateTypeCode);
		}

		public int CompareTo(EmailTemplate other)
		{
			return Subject.CompareTo(other.Subject);
		}

		public void Save()
		{
			CreatedDate = DateTime.Now;
			LastModifiedDate = DateTime.Now;
			IsDeleted = false;

			Insert();
		}

		public void UpdateInstance()
		{
			LastModifiedDate = DateTime.Now;
			
			Update();
		}

		public void MarkAsDeleted()
		{
			LastModifiedDate = DateTime.Now;
			IsDeleted = true;

			Update();
		}
	}
}

